using UnityEngine;
using System.Collections;
using System;

public class Game : MonoBehaviour
{

	public static float airplaneangley = 0.0f;
	static int gameover = 0;
	float crashforce = 0;
	static float rotationx = 0f;
	static float rotationy = 0.0f;
	static float rotationz = 0.0f;
	float positionx = 0.0f;
	static float positiony = 0.0f;
	float positionz = 0.0f;
	static float speed = 0.0f;
	float uplift = 0.0f;
	float pseudogravitation = -0.3f;
	float rightleftsoft = 0.0f;
	float rightleftsoftabs = 0.0f;
	float divesalto = 0.0f;
	float diveblocker = 0.0f;
	GameObject player;
	public Transform[] avatar = new Transform[11];
	public Transform[] cameras = new Transform[11];
	public Transform bullet;
	String playerEnter;
	int playerNum;
	Vector3 posFire;
	Transform fire;
	Transform aviao;
	Rigidbody airplane;
	public static bool played;
	float timer = 0;
	float timer2 = 0;
	public GUIText dead;
	public GUIText resp;
	bool morto = false;
	public GameObject center;
	System.Random rand = new System.Random();
	

	void noAr ()
	{
		playerEnter = Network.player.ToString ();
		playerNum = int.Parse (playerEnter);
		Network.Instantiate (avatar [playerNum], avatar [playerNum].position, avatar [playerNum].rotation, 0);
		Network.Instantiate (cameras [playerNum], avatar [playerNum].position, avatar [playerNum].rotation, 0);
		aviao = GameObject.Find ("Personagem" + playerEnter + "(Clone)").transform;
		statsManager sM = aviao.GetComponent<statsManager>();
		sM.setID(playerNum);
		airplane = aviao.rigidbody;
		airplane.rigidbody.useGravity = false;
		dead.enabled = false;
		resp.enabled = false;
		aviao.position = new Vector3 (rand.Next (40, 901), 200, rand.Next(40, 901));
		aviao.transform.LookAt(center.transform.position);
		speed = 700;
		played = true;
		Network.sendRate = 101;
	}

	void Update ()
	{
		if (played) 
		{
			Debug.Log (timer2);
			statsManager sM = aviao.GetComponent<statsManager>();
			int index = sM.getID();
			
			if (index == this.playerNum)
			{
				int hp = sM.getHealth();
				
				if (Input.GetKeyDown (KeyCode.G))
				{
					sM.changeHealth(10);
				}
				
				if(hp <= 0)
				{
					timer2 += Time.deltaTime;
					speed = 0;
					dead.enabled = true;
					resp.enabled = true;				
					
					if (timer2 >= 1)
					{
						sM.changeHealth(-100);
						aviao.position = new Vector3 (rand.Next (40, 901), 180, rand.Next(40, 901));
						aviao.transform.LookAt(center.transform.position);
						speed = 700;
						dead.enabled = false;
						resp.enabled = false;
						timer2 = 0;
					}
				}
			}	
			
			timer += Time.deltaTime;
			
			airplaneangley = aviao.eulerAngles.y; 
		

			if ((gameover == 2) && (Input.GetKey ("enter")) || (gameover == 2) && (Input.GetKey ("return"))) {
				gameover = 0;
				airplane.rigidbody.useGravity = false;
				aviao.position = new Vector3 (0, 1.67f, 0);
				aviao.eulerAngles = new Vector3 (0, 0, 0);
			}
			
			if (Input.GetKey ("space")) {
		
				if (timer >= 0.08f) {
					fire = GameObject.Find ("Fire" + playerEnter).transform;
					posFire = fire.position;
					Network.Instantiate (bullet, posFire, fire.rotation, 0);
					timer = 0;
				}
		
			}
			

			if (gameover == 0) {

				rotationx = aviao.eulerAngles.x; 
				rotationy = aviao.eulerAngles.y; 
				rotationz = aviao.eulerAngles.z; 
				positionx = aviao.position.x;
				positiony = aviao.position.y;
				positionz = aviao.position.z;

	
				if ((Input.GetAxis ("Vertical") <= 0) && ((speed > 595))) {
					aviao.Rotate ((Input.GetAxis ("Vertical") * Time.deltaTime * 80), 0, 0); 
				}

				if ((Input.GetAxis ("Vertical") > 0) && ((speed > 595))) {
					aviao.Rotate ((0.8f - divesalto) * (Input.GetAxis ("Vertical") * Time.deltaTime * 80f), 0f, 0f); 
				}
		
	
				if (groundtrigger.triggered == 1)
					aviao.Rotate (0, Input.GetAxis ("Horizontal") * Time.deltaTime * 30, 0, Space.World); 

				if (groundtrigger.triggered == 0)
					aviao.Rotate (0, Time.deltaTime * 100 * rightleftsoft, 0, Space.World); 
		
				if ((groundtrigger.triggered == 0))
					aviao.Rotate (0, 0, Time.deltaTime * 100 * (1.0f - rightleftsoftabs - diveblocker) * Input.GetAxis ("Horizontal") * -1.0f); 		

				if ((Input.GetAxis ("Horizontal") >= 0) && (rotationz > 270))
					rightleftsoft = (7.92f - rotationz * 2.2f / 100);
				if ((Input.GetAxis ("Horizontal") <= 0) && (rotationz > 0) && (rotationz < 90))
					rightleftsoft = rotationz * 2.2f / 100 * -1;
				if (rightleftsoft > 1)
					rightleftsoft = 1;
				if (rightleftsoft < -1)
					rightleftsoft = -1;
		
				if ((rightleftsoft > -0.01f) && (rightleftsoft < 0.01f))
					rightleftsoft = 0;
		
				rightleftsoftabs = Mathf.Abs (rightleftsoft);
				if (rotationx < 90)
					divesalto = rotationx / 100.0f;
				if (rotationx > 90)
					divesalto = -0.2f;
		
				if (rotationx < 90)
					diveblocker = rotationx / 200.0f;
				else
					diveblocker = 0;

				if ((rotationz < 180) && (Input.GetAxis ("Horizontal") > 0))
					aviao.Rotate (0, 0, rightleftsoft * Time.deltaTime * 80);
				if ((rotationz > 180) && (Input.GetAxis ("Horizontal") < 0))
					aviao.Rotate (0, 0, rightleftsoft * Time.deltaTime * 80);

				if (!Input.GetButton ("Horizontal")) {
					if ((rotationz < 135))
						aviao.Rotate (0, 0, rightleftsoftabs * Time.deltaTime * -100);
					if ((rotationz > 225))
						aviao.Rotate (0, 0, rightleftsoftabs * Time.deltaTime * 100);
				}
			
				if ((!Input.GetButton ("Vertical")) && (groundtrigger.triggered == 0)) {
					if ((rotationx > 0) && (rotationx < 180))
						aviao.Rotate (Time.deltaTime * -50, 0, 0);
					if ((rotationx > 0) && (rotationx > 180))
						aviao.Rotate (Time.deltaTime * 50, 0, 0);
				}
				aviao.Translate (0, 0, speed / 15 * Time.deltaTime);
		
				if ((groundtrigger.triggered == 1) && (Input.GetButton ("Fire1")) && (speed < 800))
					speed += Time.deltaTime * 240;
				if ((groundtrigger.triggered == 0) && (Input.GetButton ("Fire1")) && (speed < 800))
					speed += Time.deltaTime * 240;
				if (speed < 0)
					speed = 0;
				if ((groundtrigger.triggered == 0) && (!Input.GetButton ("Fire1")) && (!Input.GetButton ("Fire2")) && (speed > 695) && (speed < 705))
					speed = 700;
				if ((Input.GetButton ("Fire1") == false) && (Input.GetButton ("Fire2") == false) && (speed > 595) && (speed < 700))
					speed += Time.deltaTime * 240.0f;
				if ((Input.GetButton ("Fire1") == false) && (Input.GetButton ("Fire2") == false) && (speed > 595) && (speed > 700))
					speed -= Time.deltaTime * 240.0f;
		
				aviao.Translate (0, uplift * Time.deltaTime / 10.0f, 0);
				
				uplift = -700 + speed;
				if ((groundtrigger.triggered == 1) && (uplift < 0))
					uplift = 0; 
				if (speed < 595) {
					if ((sensor.sensorfront == 0) && (sensorear.sensorrear == 1))
						aviao.Rotate (Time.deltaTime * 20, 0, 0);
					if ((sensor.sensorfront == 1) && (sensorear.sensorrear == 0))
						aviao.Rotate (Time.deltaTime * -20, 0, 0);
					if (sensorfrontup.sensorfrontp == 1)
						aviao.Rotate (Time.deltaTime * -20, 0, 0);
					if (groundtrigger.triggered == 0)
						aviao.Translate (0, pseudogravitation * Time.deltaTime / 10.0f, 0);
				}
		
			}
			if (aviao.position.x >= 1000.0)
				aviao.position = new Vector3 (-50, aviao.position.y, aviao.position.z);
			else if (aviao.position.x <= -50.0f)
				aviao.position = new Vector3 (1000.0f, aviao.position.y, aviao.position.z);
			else if (aviao.position.z >= 1000.0f)
				aviao.position = new Vector3 (aviao.position.x, aviao.position.y, -50);
			else if (aviao.position.z <= -50.0f)
				aviao.position = new Vector3 (aviao.position.x, aviao.position.y, 1000.0f);
			if (positiony > 240)
				aviao.position = new Vector3 (aviao.position.x, 240, aviao.position.z);
			
			
			
			
			
		}
	}
	
	void OnGUI ()
	{
		if (Game.played) 
		{
			statsManager sM = aviao.GetComponent<statsManager>();
			int index = sM.getID();
			
			if (index == this.playerNum)
			{
				int hp = sM.getHealth();
				GUI.Box (new Rect (Screen.width - 160, 15, 150, 20), "Health: " + hp.ToString () + "%");
			}						
		}
		
	}
	
	

	
	
	
	
	
}
